package com.liuguang.testassert.security.getFilterIntercepter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @ClassName: MyAccessDecisionManager
 * @Description：
 * @Author: wanghr
 * Date: 7:37 PM 2020/6/1
 * Version: 1.0
 */
@Slf4j
@Component
public class MyAccessDecisionManager implements AccessDecisionManager {
    @Override
    public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {
        //根据url获取的权限
        log.info("根据url获取的角色",configAttributes.size());
        //获取 用户的权限
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        log.info("过滤器中设置的角色：{}",authorities);

        for (GrantedAuthority authority : authorities) {
            String  authority1 = authority.getAuthority();
            for (ConfigAttribute configAttribute : configAttributes) {
                String attribute = configAttribute.getAttribute();
                log.info("aaaaaaa:{}",attribute);
                if (Objects.equals(attribute, authority1)) {
                    log.info(attribute+"-----"+authority1);
                    return;
                }
            }
        }
        throw new RuntimeException("授权失败");
    }

    @Override
    public boolean supports(ConfigAttribute attribute) {
        return true;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return true;
    }
}
